/*
 * Copyright (c) 2017. 深圳掌控网络有限公司. All rights reserved.
 */

package com.yiqi.lottery.feature.login.presenter;

import android.text.TextUtils;
import com.suozhang.framework.entity.bo.UserAccount;
import com.suozhang.framework.entity.bo.UserInfoBo;
import com.suozhang.framework.framework.AM;
import com.suozhang.framework.utils.T;
import com.suozhang.framework.utils.ValidatorUtil;
import com.suozhang.framework.utils.logger.Logger;
import com.yiqi.lottery.entity.bo.RegisterBo;
import com.yiqi.lottery.feature.login.contract.LoginContract;
import com.yiqi.lottery.feature.login.dependencies.login.DaggerLoginPresenterComponent;
import io.reactivex.Observer;
import io.reactivex.disposables.Disposable;

import javax.inject.Inject;

/**
 *      LIJUWEN
 * @email yiyayiyayaoljw@gmail.com
 * @date 2017/4/11 9:31
 */
public class LoginPresenter implements LoginContract.Presenter {

    private LoginContract.View mView;

    @Inject

    LoginContract.Model mModel;


    public LoginPresenter(LoginContract.View mView) {
        this.mView = mView;
        DaggerLoginPresenterComponent.create().inject(this);
    }

    @Override
    public void start() {

    }

    @Override
    public void initUserAccount() {
        //获取缓存的用户帐号信息
        String userName = null;
        String password = null;
        boolean isRemember = AM.user().isRememberPassword();

        UserAccount account = AM.user().getUserAccountCache();
        if (account != null) {
            userName = account.getUsername();
            password = account.getPassword();
        }
        userName = TextUtils.isEmpty(userName) ? "" : userName;
        password = TextUtils.isEmpty(password) ? "" : password;

        //设置帐号信息
        mView.setUserAccount(userName, password, isRemember);
    }

    @Override
    public void setRememberPassword(boolean isRemember) {
        //保存记住密码状态
        AM.user().setRememberPassword(isRemember);
    }

    @Override
    public void userLogout() {
        mModel.userLogout().compose(mView.bindToLife()).subscribe(new Observer<String>() {
            @Override
            public void onSubscribe(Disposable d) {

            }

            @Override
            public void onNext(String s) {
              mView.showLogOutSuccess();
            }

            @Override
            public void onError(Throwable e) {
               mView.showLoginError(e.getMessage());
            }

            @Override
            public void onComplete() {

            }
        });
    }


    @Override
    public void doUserLogin(String mobile, String password, boolean isRember) {
        if (!ValidatorUtil.isMobile(mobile)) {
            T.showShort("手机号格式不正确！");
            return;
        }
        if (TextUtils.isEmpty(password)) {
            T.showShort("密码不能为空！");
            return;
        }

        RegisterBo registerBo = new RegisterBo(mobile, password);
        mModel.userLogin(registerBo).compose(mView.bindToLife()).subscribe(new Observer<UserInfoBo>() {
            @Override
            public void onSubscribe(Disposable d) {
                mView.showLoading(d);
            }

            @Override
            public void onNext(UserInfoBo s) {
                mView.dismissLoading();
                loginSuccess(s, isRember, new UserAccount(mobile, password));
            }

            @Override
            public void onError(Throwable e) {
                mView.dismissLoading();
                mView.showLoginError(e.getMessage());
            }

            @Override
            public void onComplete() {
                mView.dismissLoading();
            }
        });
    }



    private void loginSuccess(UserInfoBo value, boolean isRemember, UserAccount userAccount) {
        Logger.d("登录成功 ==== " + value.toString());
        T.showShort("登录成功！");
        AM.user().saveLoginResultCache(value);
        if (isRemember) {
            AM.user().saveUserAccountCache(userAccount);
        } else {
            AM.user().saveUserAccountCache(null);
        }
        mView.showLoginSuccess();
    }

}
